const https = require('https');
const fs = require('fs');
const path = require('path');

const options = {
  key: fs.readFileSync(path.join('../key.pem')),  // 读取私钥文件
  cert: fs.readFileSync(path.join('../cert.pem')), // 读取证书文件
  // 允许自签名证书
  rejectUnauthorized: false
};

const server = https.createServer(options, (req, res) => {
  if (req.url === '/') {
    // 假设你的 HTML 文件在同一目录下，名为 index.html
    const filePath = path.join('index.html');
    fs.readFile(filePath, (err, data) => {
      if (err) {
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('Server Error');
        return;
      }
      res.writeHead(200, { 'Content-Type': 'text/html' });
      res.end(data);
    });
  } else {
    res.writeHead(404, { 'Content-Type': 'text/plain' });
    res.end('Not Found');
  }
});

server.listen(3001, () => {
  console.log('HTTPS Server running at https://localhost:3001');
});

